<?php
namespace app\store\model;
use app\common\model\Notice as NoticeModel;
/**
 * 文章模型
 */
class Notice extends NoticeModel{
    /**
     * 获取公告列表
     */
    public function getList($wxapp_id){
        return $this->where('wxapp_id', '=', $wxapp_id)
            ->order(['create_time' => 'desc'])
            ->paginate(15, false,[
                'query'=>request()->request()
            ]);

    }
    /**
     * 新增公告
     */
    public function add($data){
        if(empty($data['title'])){
            $this->error='请输入标题';
            return false;
        }
        if(empty($data['content'])){
            $this->error='请输入文章内容';
            return false;
        }
        $data['wxapp_id']=self::$wxapp_id;
        return $this->allowField(true)->save($data);
    }
    /**
     * 更新公告
     */
    public function edit($data){
        if (empty($data['title'])){
            $this->error='请输入标题';
            return false;
        }
        if (empty($data['content'])){
            $this->error='请输入文章内容';
            return false;
        }
        return $this->allowField(true)->save($data)!==false;
    }
    /**
     * 软删除
     */
    public function setDelete(){
        return $this->save(['status'=>0]);
    }
    /**
     * 获取公告总数量
     */
    public static function getArticleTotal($where=[]){
        $model=new static;
        !empty($where)&&$model->where($where);
        return $model->where('wxapp_id','=', self::$wxapp_id)->count();
    }
}